{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook demonstrates how you can calculate reaction energies using the Materials API and pymatgen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Caculated\n",
      "1.000 CaO + 1.000 CO2 -> 1.000 CaCO3\n",
      "Reaction energy = -145.39165567849665 kJ mol^-1\n",
      "\n",
      "Experimental\n",
      "1.000 CaO + 1.000 CO2 -> 1.000 CaCO3\n",
      "Reaction energy = -178.3 kJ mol^-1\n"
     ]
    }
   ],
   "source": [
    "from pymatgen.matproj.rest import MPRester\n",
    "from pymatgen import Composition\n",
    "from pymatgen.entries.computed_entries import ComputedEntry\n",
    "from pymatgen.core.units import FloatWithUnit\n",
    "from pymatgen.analysis.reaction_calculator import ComputedReaction\n",
    "\n",
    "#This initializes the REST adaptor. Put your own API key in.\n",
    "a = MPRester(\"YOUR_API_KEY\")\n",
    "\n",
    "#This gets all entries belonging to the Ca-C-O system.\n",
    "all_entries = a.get_entries_in_chemsys(['Ca', 'C', 'O'])\n",
    "\n",
    "#This method simply gets the lowest energy entry for all entry with the same composition.\n",
    "def get_most_stable_entry(formula):\n",
    "    relevant_entries = [entry for entry in all_entries if entry.composition.reduced_formula == Composition(formula).reduced_formula]\n",
    "    relevant_entries = sorted(relevant_entries, key=lambda e: e.energy_per_atom)\n",
    "    return relevant_entries[0]\n",
    "\n",
    "CaO = get_most_stable_entry(\"CaO\")\n",
    "CO2 = get_most_stable_entry(\"CO2\")\n",
    "CaCO3 = get_most_stable_entry(\"CaCO3\")\n",
    "\n",
    "reaction = ComputedReaction([CaO, CO2], [CaCO3])\n",
    "energy = FloatWithUnit(reaction.calculated_reaction_energy, \"eV atom^-1\")\n",
    "\n",
    "print \"Caculated\"\n",
    "print reaction\n",
    "print \"Reaction energy = {}\".format(energy.to(\"kJ mol^-1\"))\n",
    "print\n",
    "\n",
    "# The following portions demonstrate how to get the experimental values as well.\n",
    "exp_CaO = a.get_exp_entry(\"CaO\")\n",
    "exp_CaCO3 = a.get_exp_entry(\"CaCO3\")\n",
    "\n",
    "#Unfortunately, the Materials Project database does not have gas phase experimental entries. This is the value from NIST. We manually create the entry.\n",
    "#Exp entries should be in kJ/mol.\n",
    "exp_CO2 = ComputedEntry(\"CO2\", -393.51)\n",
    "\n",
    "exp_reaction = ComputedReaction([exp_CaO, exp_CO2], [exp_CaCO3])\n",
    "\n",
    "print \"Experimental\"\n",
    "print exp_reaction\n",
    "print \"Reaction energy = {} kJ mol^-1\".format(exp_reaction.calculated_reaction_energy)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
